package com.yisu.expressway.settings;

import android.content.Context;
import android.content.SharedPreferences;

public class AppGlobalSetting {
    private static final String DEFAULT_GLOBALSETTING = "AppGlobalSetting";
    private Context context;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public AppGlobalSetting(Context ctx) {
        this(ctx, DEFAULT_GLOBALSETTING, Context.MODE_PRIVATE);
    }

    public AppGlobalSetting(Context ctx, String name, int mode) {
        context = ctx.getApplicationContext();
        preferences = context.getSharedPreferences(name, mode);
        editor = preferences.edit();
    }

    public boolean getBooleanGlobalItem(String key, boolean defaultValue){
        return preferences.getBoolean(key, defaultValue);
    }

    public void setBooleanGlobalItem(String key, boolean value, boolean commit) {
        editor.putBoolean(key, value);
        if (commit) {
            editor.commit();
        }
    }

    public void setBooleanGlobalItem(String key, boolean value) {
        setBooleanGlobalItem(key, value, true);
    }

    public String getStringGlobalItem(String key, String defaultValue) {
        return preferences.getString(key, defaultValue);
    }

    public void setStringGlobalItem(String key, String value, boolean commit) {
        editor.putString(key, value);
        if (commit) {
            editor.commit();
        }
    }

    public void setStringGlobalItem(String key, String value) {
        setStringGlobalItem(key, value, true);
    }

    public int getIntGlobalItem(String key, int defaultValue) {
        return preferences.getInt(key, defaultValue);
    }

    public void setIntGlobalItem(String key, int value, boolean commit) {
        editor.putInt(key, value);
        if (commit) {
            editor.commit();
        }
    }

    public void setIntGlobalItem(String key, int value) {
        setIntGlobalItem(key, value, true);
    }

    public long getLongGlobalItem(String key, long defaultValue) {
        return preferences.getLong(key, defaultValue);
    }

    public void setLongGlobalItem(String key, long value, boolean commit) {
        editor.putLong(key, value);
        if (commit) {
            editor.commit();
        }
    }

    public void setLongGlobalItem(String key, long value) {
        setLongGlobalItem(key, value, true);
    }

    public float getFloatGlobalItem(String key, float defaultValue) {
        return preferences.getFloat(key, defaultValue);
    }

    public void setFloatGlobalItem(String key, float value, boolean commit) {
        editor.putFloat(key, value);
        if (commit) {
            editor.commit();
        }
    }

    public void setFloatGlobalItem(String key, float value) {
        setFloatGlobalItem(key, value, true);
    }

    /**
     * 检测是否存在这个key
     * @param key
     * @return
     */
    public boolean hasKey (String key) {
        return preferences.contains(key);
    }

    public void removeKey(String key){
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.apply();
    }

    public void clearAllSharePreferenceData(){
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}
